﻿using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using _4.Model;

namespace _4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitialFileTree();
            InitialImageList();
            InitialContextMenu();
        }

        private void InitialFileTree()
        {
            //清空树
            fileTree.Nodes.Clear();
            fileTree.Nodes.Add("C:\\");
            fileTree.Nodes[0].Tag = new FileSystemTag("C:\\", true);
            ScanFolder(fileTree.Nodes[0]);
        }

        private void InitialImageList()
        {
            var imageList = new ImageList();
            imageList.Images.Add("folder", Properties.Resources.folder);
            imageList.Images.Add("file", Properties.Resources.file);
            listView1.LargeImageList = imageList;
        }

        private void InitialContextMenu()
        {
            var contextMenu = new ContextMenuStrip();
            var openMenuItem = new ToolStripMenuItem("打开");
            openMenuItem.Click += listView1_DoubleClick;
            contextMenu.Items.Add(openMenuItem);
            listView1.ContextMenuStrip = contextMenu;
        }

        private void ScanFolder(TreeNode node)
        {
            var tag = node.Tag as FileSystemTag;
            if (tag == null || !tag.IsFolder)
            {
                return;
            }

            var path = tag.Path;
            //获取所有子文件夹
            var dirs = Directory.GetDirectories(path);
            //过滤掉系统文件夹和隐藏文件夹
            dirs =dirs.Where(d=>(new DirectoryInfo(d).Attributes & (FileAttributes.System | FileAttributes.Hidden)) == 0).ToArray();
            foreach (var dir in dirs)
            {
                var dirNode = node.Nodes.Add(Path.GetFileName(dir));
                dirNode.Tag = new FileSystemTag(dir, true);
                try {
                    if (Directory.GetDirectories(dir).Length > 0)
                    {
                        dirNode.Nodes.Add("");
                    }
                }catch(Exception e)
                {
                    MessageBox.Show(e.Message);
                }
                
            }
        }


        //更新下级内容，无论是展开还是双击打开
        private void fileTree_AfterExpand(object sender, TreeViewEventArgs e)
        {
            var selectedNode = e.Node;
            selectedNode.Nodes.Clear();
            ScanFolder(selectedNode);
        }

        //双击打开文件夹，在右侧显示文件夹内容
        private void fileTree_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Tag is FileSystemTag tag) UpdateListView(tag.Path);
        }

        private void UpdateListView(string path)
        {
            listView1.Items.Clear();
            try
            {
                var dirs = Directory.GetDirectories(path);

                dirs = dirs.Where(d => (new DirectoryInfo(d).Attributes & (FileAttributes.System | FileAttributes.Hidden)) == 0).ToArray();
                foreach (var dir in dirs)
                {
                    var item = listView1.Items.Add(Path.GetFileName(dir), "folder");
                    item.Tag = new FileSystemTag(dir, true);
                }
                var files = Directory.GetFiles(path);
                foreach (var file in files)
                {
                    var item = listView1.Items.Add(Path.GetFileName(file), "file");
                    item.Tag = new FileSystemTag(file, false);
                }
            }catch(Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) return;
            var tag = listView1.SelectedItems[0].Tag as FileSystemTag;
            if (tag == null) return;
            if (tag.IsFolder)
            {
                UpdateListView(tag.Path);
            }
            else
            {
                System.Diagnostics.Process.Start(tag.Path);
            }
        }
    }
}